import Vue from 'vue'
import Vuex from 'vuex'
import { setStorage, getStorage } from '@/storage'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    refresh_token: getStorage('refresh_token'),
    token: getStorage('token'),
    user: JSON.parse(getStorage('user')) || {} // 用户-基本信息 (因为如果本地没有值, 就是null, 你用null访问photo就报错, 所以如果前者为null, 我们就给一个{})
  },
  mutations: {
    clearToken (state) { // 清除token
      state.token = ''
    },
    clearRefreshToken (state) {
      state.refresh_token = ''
    },
    setRefreshToken (state, refresh_token) {
      state.refresh_token = refresh_token
      setStorage('refresh_token', refresh_token) // 页面刷新 state值清空了
    },
    setToken (state, token) {
      state.token = token
      setStorage('token', token)
    },
    setUser (state, payLoad) {
      state.user = payLoad
      setStorage('user', JSON.stringify(payLoad)) // 将用户信息保存到本地
    }

  },
  actions: {
  },
  modules: {
  }
})
